<?php
class User_related_self_model extends CI_Model
{
    private $table   = 'user_related_self';
    private $table_2 = 'user';
    
    public function findById($id)
    {
        $this->db->where('id', $id);
        return $this->db->get($this->table);
    }
    
    public function total($params = array())
    {
        $this->db->from($this->table . ' AS user_related_self');
        $this->db->join($this->table_2 . ' AS user', 'user.uid = user_related_self.provider_uid');
        if (!empty($params['provider_uid'])) {
            $this->db->where("((`user`.`user_name` LIKE '%{$params['provider_uid']}%') OR (`user`.`alias_name` LIKE '%{$params['provider_uid']}%'))");
        }
        if (!empty($params['reseller_uid'])) {
            $this->db->where('user_related_self.reseller_uid', $params['reseller_uid']);
        }
        if (!empty($params['flag'])) {
            $this->db->where('user_related_self.flag', $params['flag']);
        }
        if (!empty($params['start_date'])) {
            $this->db->where('user_related_self.created_at >= ', $params['start_date'].' 00:00:00');
        }
        if (!empty($params['end_date'])) {
            $this->db->where('user_related_self.created_at <= ', $params['end_date'].' 23:59:59');
        }
        return $this->db->count_all_results();
    }
    
    public function page_list($page_num, $num, $params = array())
    {
        $this->db->select('user_related_self.*, user.user_name, user.alias_name, user.mobile_phone, user.user_type, user.parent_id');
        $this->db->from($this->table . ' AS user_related_self');
        $this->db->join($this->table_2 . ' AS user', 'user.uid = user_related_self.provider_uid');
        if (!empty($params['provider_uid'])) {
            $this->db->where("((`user`.`user_name` LIKE '%{$params['provider_uid']}%') OR (`user`.`alias_name` LIKE '%{$params['provider_uid']}%'))");
        }
        if (!empty($params['reseller_uid'])) {
            $this->db->where('user_related_self.reseller_uid', $params['reseller_uid']);
        }
        if (!empty($params['flag'])) {
            $this->db->where('user_related_self.flag', $params['flag']);
        }
        if (!empty($params['start_date'])) {
            $this->db->where('user_related_self.created_at >= ', $params['start_date'].' 00:00:00');
        }
        if (!empty($params['end_date'])) {
            $this->db->where('user_related_self.created_at <= ', $params['end_date'].' 23:59:59');
        }
        $this->db->order_by('id', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }
    
    public function insertUserRelatedSelf($postData = array())
    {
        $data = array(
            'provider_uid' => trim($postData['provider_uid']),
            'reseller_uid' => trim($postData['reseller_uid']),
            'poundage'     => trim($postData['poundage']),
            'flag'         => !empty($postData['flag']) ? $postData['flag'] : 1,
            'created_at'   => date('Y-m-d H:i:s'),
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function updateUserRelatedSelf($postData = array())
    {
        $data = array(
            'provider_uid' => trim($postData['provider_uid']),
            'reseller_uid' => trim($postData['reseller_uid']),
            'poundage'     => trim($postData['poundage']),
            'flag'         => !empty($postData['flag']) ? $postData['flag'] : 1,
        );
        $this->db->where('id', $postData['id']);
        return $this->db->update($this->table, $data);
    }
    
    public function toggle($id, $flag)
    {
        $data = array('flag'=> $flag);
        $this->db->where('id', $id);
        return $this->db->update($this->table, $data);
    }
    
    public function delete($id){
    	
        $this->db->where('id', $id);
        return $this->db->delete($this->table);
    }
    
     /**
     * 检查供应商是否存在
     * @param unknown $provider_uid
     */
    public function providerUidRepeat($provider_uid){
    	
        $this->db->where('provider_uid',$provider_uid);
        return $this->db->get($this->table);
    }
}